package net.huashitong.user.web;

import net.huashitong.user.entity.Role;
import net.huashitong.user.filter.RoleFilter;
import net.huashitong.user.service.RoleService;
import net.huashitong.zhApp.controller.BaseCtrl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(value = "/api/role")
public class RoleController extends BaseCtrl {

    @Autowired
    private RoleService service;

    /**
     * 查询
     *
     * @param filter
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/queryByFilter", method = RequestMethod.POST)
    public Map<String, Object> queryByFilter(@RequestBody RoleFilter filter) {
        List<Role> list = service.queryByFilter(filter);
        return coverMessage("200", "查询成功", list);
    }

    /**
     * 查询不分页
     *
     * @param filter
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/queryByFilterNoPage", method = RequestMethod.POST)
    public Map<String, Object> queryByFilterNoPage(@RequestBody RoleFilter filter) {
        List<Role> list = service.queryByFilterNoPage(filter);
        return coverMessage("200", "查询成功", list);
    }

    /**
     * 添加
     *
     * @param filter
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/addDepartment", method = RequestMethod.POST)
    public Map<String, Object> addDepartment(@RequestBody RoleFilter filter) {
        service.addRole(filter);
        return coverMessage("200", "添加成功");
    }

    /**
     * 修改
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/editDepartment", method = RequestMethod.POST)
    public Map<String, Object> editDepartment(@RequestBody Role newRole) throws NoSuchFieldException {
        Role oldRole = service.getById(newRole.getSysId());
        service.updateRole(oldRole, newRole);
        return coverMessage("200", "修改成功");
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/delete/{id}", method = RequestMethod.GET)
    public Map<String, Object> deleteDepartment(@PathVariable List<String> id) {
        service.batchDelete(id);
        return coverMessage("200", "删除成功");
    }

    /**
     * 查询单位名称是否已存在
     *
     * @param roleName
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/queryRoleNameExist", method = RequestMethod.GET)
    public Map<String, Object> queryRoleNameExist(@RequestParam String roleName) {
        boolean result = service.queryRoleExist(roleName);
        if (result) {
            return coverMessage("1", "该单位已存在");
        } else {
            return coverMessage("0", "该单位不存在");
        }
    }


}
